package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebBannerDao;
import ysq.xyg.web.model.po.WebBanner;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebBannerVo;
import ysq.xyg.web.service.WebBannerService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-网站banner图service实现
 *
 * @author shengQiang yu
 * @date 2019-6-5 21:55:58
 */
@Service("webBannerService")
public class WebBannerServiceImpl extends BaseServiceImpl<WebBanner> implements WebBannerService {

    @Resource
    private WebBannerDao webBannerDao;

    private WebBanner webBanner;

    @Autowired
    public void setBaseDao(WebBannerDao webBannerDao) {
        this.baseDao = webBannerDao;
    }


    /**
     * 分页查询
     *
     * @param webBannerVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 21:55:58
     */
    @Override
    public ReturnEntity page(WebBannerVo webBannerVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<WebBanner> list = acceptList(webBannerVo);
        PageInfo<WebBanner> page=new PageInfo<WebBanner>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webBannerVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 21:55:58
     */
    @Override
    public ReturnEntity addOrUpdate(WebBannerVo webBannerVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webBannerVo.getId())){
            webBanner=new WebBanner(currentUser);
            isUpate=false;
        }else{
            webBanner=selectByPrimaryKey(webBannerVo.getId());
            if(ObjectUtil.isNull(webBanner)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webBanner.setUpdateTime(new Date());
            webBanner.setUpdateUser(currentUser.getId());
        }
        webBanner.setBannerCode(webBannerVo.getBannerCode());
        webBanner.setUseType(webBannerVo.getUseType());
        webBanner.setAuthor(webBannerVo.getAuthor());
        webBanner.setImage(webBannerVo.getImage());
        webBanner.setDescription(webBannerVo.getDescription());
        webBanner.setSort(webBannerVo.getSort());
        webBanner.setShowType(webBannerVo.getShowType());
        webBanner.setUserId(currentUser.getId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webBanner);
        }else{
            num=insert(webBanner);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-5 21:55:58
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebBanner webBanner=selectByPrimaryKey(id);
        if(webBanner!=null){
            webBanner.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webBanner.setUpdateTime(new Date());
            webBanner.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webBanner);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webBannerVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-6-5 21:55:58
     */
    @Override
    public List<WebBanner> acceptList(WebBannerVo webBannerVo){
        Example example = new Example(WebBanner.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webBannerVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webBannerVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webBannerVo.getBannerCode())){
            criteria.andEqualTo("bannerCode",webBannerVo.getBannerCode());
        }
        if(StringUtil.isNotEmpty(webBannerVo.getUseType())){
            criteria.andEqualTo("useType",webBannerVo.getUseType());
        }
        if(StringUtil.isNotEmpty(webBannerVo.getAuthor())){
            criteria.andLike("author","%"+webBannerVo.getAuthor()+"%");
        }
        if(StringUtil.isNotEmpty(webBannerVo.getShowType())){
            criteria.andEqualTo("showType",webBannerVo.getShowType());
        }
        if(StringUtil.isNotEmpty(webBannerVo.getUserId())){
            criteria.andEqualTo("userId",webBannerVo.getUserId());
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return webBannerDao.selectByExample(example);
    }
}
